import { createPinia, defineStore } from 'pinia'
import { Loginuser } from '../api/login_api'
import { notification } from "ant-design-vue";
import router from '../router/index'
import { getUsers } from '../api/user_api'

export const appStore = defineStore('appStore', {
    state: () => {
        return {
            access_token: '',  //token
            userInfo:{
                userId:'', //id
                username:'', //用户名
                roles:[],
                status:Boolean,
                avatar:String
            },
            title:'',
        }          
    },
    actions: {
        // 1,Login登录
        Login_user(val: any) {
            Loginuser({ username: val.username, password: val.password }).then((res: any) => {
                this.access_token = res.data.access_token
                // console.log(res);
                if (res.status == '201') {
                    router.replace('/home')
                    notification["success"]({
                        message: "登陆成功",
                        description:
                            "恭喜你登陆成功",
                    });
                }
            })
        },
        //用户信息
        USERIfo() {
            getUsers().then((res: any) => {
                console.log(res);
                this.userInfo = res.data
            })
        },
        //面包屑
        TITLE(val:any){
            console.log(val);
            this.title = val
        }
    },
    getters: {
        TOKEN(): string {
            return this.access_token
        }
    },
    persist: {
        storage: window.sessionStorage
    }
})